package test.marui.com.mytest.view.slideBackView.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;

import test.marui.com.mytest.utils.DisplayUtil;

/**
 * Created by MR on 2019/2/20.
 */
public class DefaultSlideView implements ISlideView {

    private int backViewColor = 0xff000000;
    private int arrowColor = Color.WHITE;
    private int width;
    private int height;
    private int arrowHeight;

    private Path bezierPath, arrowPath;
    private Paint paint, arrowPaint;

    public DefaultSlideView(Activity activity) {
        Context context = activity;
        width = DisplayUtil.dip2px(context, 50);
        height = DisplayUtil.dip2px(context, 200);
        arrowHeight = DisplayUtil.dip2px(context, 4);
        init(context);
    }

    public void setBackViewColor(int backViewColor) {
        this.backViewColor = backViewColor;
    }

    public void setArrowColor(int arrowColor) {
        this.arrowColor = arrowColor;
    }

    private void init(Context context) {
        bezierPath = new Path();
        arrowPath = new Path();

        paint = new Paint();
        paint.setColor(backViewColor);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(DisplayUtil.dip2px(context, 1.5f));

        arrowPaint = new Paint();
        arrowPaint.setColor(arrowColor);
        arrowPaint.setAntiAlias(true);
        arrowPaint.setStyle(Paint.Style.STROKE);
        arrowPaint.setStrokeWidth(DisplayUtil.dip2px(context, 1.5f));
        arrowPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    public boolean scrollVertical() {
        return true;
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public void onDraw(Canvas canvas, float currentWidth) {
        int height = getHeight();
        int maxWidth = getWidth();
        float centerY = height / 2;

        Log.e("draw",currentWidth+"");
        float progress = currentWidth / maxWidth;
        if (progress == 0) {
            return;
        }

        paint.setColor(backViewColor);
        paint.setAlpha((int) (200 * progress));

        // 画贝塞尔曲线
        float bezierWidth = currentWidth / 2;
        bezierPath.reset();
        bezierPath.moveTo(0, 0);
        bezierPath.cubicTo(0, height / 4, bezierWidth, height * 3f / 8, bezierWidth, centerY);
        bezierPath.cubicTo(bezierWidth, height * 5f / 8, 0, height * 3f / 4, 0, height);
        canvas.drawPath(bezierPath, paint);


        arrowPaint.setColor(arrowColor);
        arrowPaint.setAlpha((int) (255 * progress));

        // 画箭头
        if (progress <= 0.2) {
            return;
        }
        float arrowLeft = currentWidth / 6;
        arrowPath.reset();
        if (progress <= 0.7) {
            float newProgress = (progress - 0.2f) / 0.5f;
            arrowPath.moveTo(arrowLeft, centerY - arrowHeight * newProgress);
            arrowPath.lineTo(arrowLeft, centerY + arrowHeight * newProgress);
            canvas.drawPath(arrowPath, arrowPaint);
        } else {
            float arrowEnd = arrowLeft + arrowHeight * ((progress - 0.7f) / 0.3f);
            arrowPath.moveTo(arrowEnd, centerY - arrowHeight);
            arrowPath.lineTo(arrowLeft, centerY);
            arrowPath.lineTo(arrowEnd, centerY + arrowHeight);
            canvas.drawPath(arrowPath, arrowPaint);
        }


    }
}
